package system.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;

import DB.DB_connection_pool;

public class Admin_login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		JSONObject jsonObj = new JSONObject();
		HttpSession session = request.getSession();
		
		String admin_account = request.getParameter("admin_account");
		String admin_password = request.getParameter("admin_password");
		
		String select_sql = "select * from admin_login where admin_account = '"+admin_account+"' and admin_password = '"+admin_password+"'";
		
		String admin_account_name = "admin";
		int weight_Jurisdiction = 0;   //权值管理权限
		int product_Jurisdiction = 0;    //产品管理权限
		int basic_jurisdiction = 0;    //动态管理权限
		int super_Jurisdiction = 0;      //超级权限
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select = conn.prepareStatement(select_sql);
			ResultSet rs = ps_select.executeQuery();
			if(rs.next()){
				admin_account_name = rs.getString("admin_account_name");
				weight_Jurisdiction = rs.getInt("weight_Jurisdiction");
				product_Jurisdiction = rs.getInt("product_Jurisdiction");
				basic_jurisdiction = rs.getInt("basic_jurisdiction");
				super_Jurisdiction = rs.getInt("super_Jurisdiction");
				
				jsonObj.put("result", true);
				
				session.setAttribute("admin_account_name", admin_account_name);
				session.setAttribute("weight_Jurisdiction", weight_Jurisdiction);
				session.setAttribute("product_Jurisdiction", product_Jurisdiction);
				session.setAttribute("basic_jurisdiction", basic_jurisdiction);
				session.setAttribute("super_Jurisdiction", super_Jurisdiction);
				
				
			}else{
				jsonObj.put("result", false);
			}
			
			rs.close();
			ps_select.close();
			conn.close();
		} catch (SQLException e) {
			jsonObj.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		out.print(jsonObj.toString());
		
	}

}
